FROM gliderlabs/alpine:3.4

USER root
# needed for `shadow` package.
RUN echo http://dl-2.alpinelinux.org/alpine/edge/community/ >> /etc/apk/repositories

# bash: for running the guix installation script
# expect: for running the guix installation script without interaction
# gnupg: for checking signatures
# shadow: for having groupadd, useradd and other commands
# wget: for downloading files
# tar: for upgrading the tar version to handle some parameters
# xz: for tar to be able to extract .tar.xz files
# Look here for packages: https://pkgs.alpinelinux.org/packages?name=xz&branch=edge
RUN apk --update upgrade
RUN apk add --no-cache \
    ca-certificates \
    bash \
    expect \
    gnupg \
    shadow \
    wget \
    tar \
    xz
RUN update-ca-certificates
# Cleanup
RUN rm -rf /var/cache/apk/*

ARG GUIX_SYSTEM="x86_64-linux"
ARG GUIX_VERSION="0.16.0"

# do stuff in /tmp
WORKDIR /tmp
# download the tar ball
RUN wget "https://alpha.gnu.org/gnu/guix/guix-binary-${GUIX_VERSION}.${GUIX_SYSTEM}.tar.xz"
# get the signatures for Guix
RUN wget "https://alpha.gnu.org/gnu/guix/guix-binary-${GUIX_VERSION}.${GUIX_SYSTEM}.tar.xz.sig"

# Note: Network issues when using `pool.sks-keyservers.net` instead of
# `ipv4.pool.sks-keyservers.net`. There is no ipv6 connectivity inside
# the container it seems. For the issue check here:
# https://github.com/nodejs/docker-node/issues/380
ARG KEYSERVER=ipv4.pool.sks-keyservers.net
RUN gpg \
    --keyserver "${KEYSERVER}" \
    --recv-keys 3CE464558A84FDC69DB40CFB090B11993D9AEBB5

# verify the signatures
RUN gpg --verify "guix-binary-${GUIX_VERSION}.${GUIX_SYSTEM}.tar.xz.sig"

# become root user
USER root
RUN ls -al
# create the GNU store
RUN tar --warning=no-timestamp -xf "guix-binary-${GUIX_VERSION}.${GUIX_SYSTEM}.tar.xz"
RUN rm "guix-binary-${GUIX_VERSION}.${GUIX_SYSTEM}.tar.xz"
RUN mv var/guix /var/ && mv gnu /

# CONTINUE: https://www.gnu.org/software/guix/manual/en/html_node/Binary-Installation.html
# make profile available
mkdir -p ~root/.config/guix
ln -sf /var/guix/profiles/per-user/root/current-guix \
    ~root/.config/guix/current


COPY guix-install.sh /
COPY install.sh /
RUN chmod +x /guix-install.sh
RUN chmod +x /install.sh

RUN expect /install.sh
